unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, jpeg, RzBckgnd, StdCtrls, RzStatus, Math, ActnList,
  StrUtils, Menus, menu;

Const
    WM_ICONBACK=WM_USER+1;

//{$DEFINE DEBUG}
type
  Tmainfrm = class(TForm)
    HeadImage: TImage;
    BottomImage: TImage;
    LeftImage: TImage;
    RightImage: TImage;
    PalCtrl: TPanel;
    Image1: TImage;
    Image2: TImage;
    labClose: TLabel;
    pal_frmparent: TPanel;
    MarqueeStatus: TRzMarqueeStatus;
    RzClockStatus: TRzClockStatus;
    Image3: TImage;
    labMin: TLabel;
    Timer: TTimer;
    Label1: TLabel;
    Image22: TImage;
    SaveDialog1: TSaveDialog;
    labStatus: TLabel;
    icoPop: TPopupMenu;
    N11: TMenuItem;
    N4: TMenuItem;
    N21: TMenuItem;
    N31: TMenuItem;
    N3: TMenuItem;
    N41: TMenuItem;
    N5: TMenuItem;
    N51: TMenuItem;
    N61: TMenuItem;
    N1: TMenuItem;
    N71: TMenuItem;
    N91: TMenuItem;
    N2: TMenuItem;
    N82: TMenuItem;
    N81: TMenuItem;
    syscmd: TActionList;
    actclose: TAction;
    actMin: TAction;
    actMax: TAction;
    actSysSet: TAction;
    actWkMng: TAction;
    actPntMng: TAction;
    actIssue: TAction;
    actPayment: TAction;
    actSummRpt: TAction;
    actGraphQc: TAction;
    actDataBackup: TAction;
    actAbout: TAction;
    Background: TRzBackground;
    pal_menu: TPanel;
    btnMenu9: TbtnMenu;
    btnMenu1: TbtnMenu;
    btnMenu2: TbtnMenu;
    btnMenu3: TbtnMenu;
    btnMenu4: TbtnMenu;
    btnMenu5: TbtnMenu;
    btnMenu6: TbtnMenu;
    btnMenu7: TbtnMenu;
    btnMenu8: TbtnMenu;
    labsftname: TLabel;
    labCompanyName: TLabel;
    procedure FormResize(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure labCloseMouseEnter(Sender: TObject);
    procedure labCloseMouseLeave(Sender: TObject);
    procedure pal_frmparentResize(Sender: TObject);
    procedure labMinMouseEnter(Sender: TObject);
    procedure labMinMouseLeave(Sender: TObject);
    procedure actMinExecute(Sender: TObject);
    procedure actcloseExecute(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure N81Click(Sender: TObject);
    procedure btnMenu1Click(Sender: TObject);
    procedure btnMenu2Click(Sender: TObject);
    procedure btnMenu3Click(Sender: TObject);
    procedure btnMenu4Click(Sender: TObject);
    procedure btnMenu5Click(Sender: TObject);
    procedure btnMenu6Click(Sender: TObject);
    procedure btnMenu7Click(Sender: TObject);
    procedure btnMenu8Click(Sender: TObject);
    procedure btnMenu9Click(Sender: TObject);
    procedure actSysSetExecute(Sender: TObject);
    procedure actWkMngExecute(Sender: TObject);
    procedure actPntMngExecute(Sender: TObject);
    procedure actIssueExecute(Sender: TObject);
    procedure actPaymentExecute(Sender: TObject);
    procedure actSummRptExecute(Sender: TObject);
    procedure actGraphQcExecute(Sender: TObject);
    procedure actDataBackupExecute(Sender: TObject);
    procedure actAboutExecute(Sender: TObject);
    procedure btnMenu9LabMenuClick(Sender: TObject);

  private
    { Private declarations }
    private sCurrStatus:string;
    procedure CurStatus(value:string);
    procedure WMICONBACK(var msg:TMessage);Message  WM_ICONBACK;
    procedure LoginGetRFID(Sender: TObject);
    procedure IssueGetRFID(Sender: TObject);
  public
    { Public declarations }

  published
    { published declarations }
    property CurrStatus:string read sCurrStatus write CurStatus;

  end;

var
  mainfrm: Tmainfrm;

implementation

{$R *.dfm}

uses
  shellapi, clxs, worker, Punnet, Records, Issue;

const
  HeadImageHeight=75;
  BottomImageHeight=105;
  LeftImageWidth=31;
  RightImageWidth=20;
  OWidth=1024;
  OHeight=768;
  PalCtrlWidth=174;
  PalCtrlHeight=584;
  CloseLeft=961;
  CloseTop=14;
  CloseWidth=50;
  MinLeft=911;
  MinWidth=26;
  ClockLeft=858;
  ICO_ID=10;

  sIcoHint='packing house system(v1.0.0) Kunshan China';

var
  LoginComm:TComm;
  IssueComm:TComm;

procedure Tmainfrm.WMICONBACK(var msg:TMessage);
var
  nIco:TNotifyIconData;
  pPos:TPoint;
begin
  if msg.LParam=WM_LBUTTONDOWN then  begin
    nIco.cbSize:=  SizeOf( TNotifyIconData );
    nIco.Wnd:=Handle;
    nIco.hIcon:=MainFrm.Icon.Handle;
    nIco.uID:=ICO_ID;
    nIco.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
    nIco.uCallbackMessage:=WM_ICONBACK;
    nIco.szTip:=sIcoHint;
    Shell_NotifyIcon(NIM_DELETE,@nIco);
    Self.Show;
  end else if msg.LParam=WM_RBUTTONDOWN then  begin
    GetCursorPos(pPos);
    icoPop.Popup(pPos.X,pPos.Y);
  end;
end;

procedure Tmainfrm.FormResize(Sender: TObject);
begin
  leftImage.Width:=Round(LeftImageWidth*ClientWidth/OWidth);
  RightImage.Width:=Round(RightImageWidth*ClientWidth/OWidth);
  Image2.Top:=CloseTop;
  Image2.Left:=Round(CloseLeft*ClientWidth/OWidth);
  Image2.Width:=Round(CloseWidth*ClientWidth/OWidth);
  
  Image3.Top:=CloseTop;
  Image3.Left:=Round(MinLeft*ClientWidth/OWidth);
  Image3.Width:=Round(MinWidth*ClientWidth/OWidth);

  labClose.Top:=CloseTop;
  labClose.Left:=Image2.Left;
  labclose.Width:=Image2.Width;

  labMin.Top:=CloseTop;
  labMin.Left:=Image3.Left;
  labMin.Width:=Image3.Width; 

  MarqueeStatus.Left:=15;
  MarqueeStatus.Width:=ClientWidth-30;
  MarqueeStatus.Top:=ClientHeight-MarqueeStatus.Height-3;
  RzClockStatus.Width:=Round(170*ClientWidth/OWidth);
  RzClockStatus.Left:=ClientWidth-RzClockStatus.Width-3;
  RzClockStatus.Top:= ClientHeight-80;
  left:=0;
  top:=0;
end;

procedure Tmainfrm.CurStatus(value:string);
begin
  sCurrStatus:=value;
  labStatus.Caption:=sCurrStatus;
end;

procedure Tmainfrm.Image1Click(Sender: TObject);
begin
  if palCtrl.Width >9 then begin
    palCtrl.Width:=9;
    //while palCtrl.Width>9 do
    //  palCtrl.Width:=palCtrl.Width-2;
  end else begin
    palCtrl.Width:=174;
    //while palCtrl.Width<174 do
    //  palCtrl.Width:=palCtrl.Width+2;
  end;
  palCtrl.Left:=rightimage.Left-palCtrl.Width;
end;

procedure Tmainfrm.labCloseMouseEnter(Sender: TObject);
begin
  Image2.Show;
end;

procedure Tmainfrm.labCloseMouseLeave(Sender: TObject);
begin
  Image2.Hide;
end;

procedure Tmainfrm.pal_frmparentResize(Sender: TObject);
begin
  labCompanyName.Width:=pal_frmparent.ClientWidth;
  labsftname.Width:=pal_frmparent.ClientWidth;
  labCompanyName.Top:=Round(136*pal_frmparent.ClientHeight/PalCtrlHeight);
  labsftname.Top:=Round(256*pal_frmparent.ClientHeight/PalCtrlHeight);
end;

procedure Tmainfrm.labMinMouseEnter(Sender: TObject);
begin
  Image3.Show;
end;

procedure Tmainfrm.labMinMouseLeave(Sender: TObject);
begin
  Image3.Hide;
end;

procedure Tmainfrm.actMinExecute(Sender: TObject);
var
  nIco:TNotifyIconData ;
begin
  {$IFNDEF DEBUG}
  nIco.cbSize:=  SizeOf( TNotifyIconData );
  nIco.Wnd:=Handle;
  nIco.hIcon:=MainFrm.Icon.Handle;
  nIco.uID:=ICO_ID;
  nIco.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
  nIco.uCallbackMessage:=WM_ICONBACK;
  nIco.szTip:=sIcoHint;
  Shell_NotifyIcon(NIM_ADD,@nIco);
  self.Hide;
  {$ELSE}
  close;
  {$ENDIF}
end;

procedure Tmainfrm.actcloseExecute(Sender: TObject);
begin
  close;
end;

procedure Tmainfrm.LoginGetRFID(Sender: Tobject);
var
  worker:TWorker;
begin
  worker:=TWorker.Create(LoginComm.CurrRFID);
  //
  //
  worker.Name:='Clark.Ma';
  LoginComm.SendStrToComm(Format('RF%12s',[worker.Name]));
  worker.startWork;
  worker.Free;
end;

procedure Tmainfrm.IssueGetRFID(Sender: TObject);
var
  worker:TWorker;
begin
  worker:=TWorker.Create(IssueComm.CurrRFID);
  //
  //
  worker.Name:='Clark.Ma';
  IssuePunnet(pal_frmparent,worker.RFID);
  IssueComm.SendStrToComm(Format('RF%12s',[worker.Name]));
  worker.GetPunnets;
  worker.Free;
end;

procedure Tmainfrm.FormCreate(Sender: TObject);
begin
  if not FileExists(ExtractFilePath(Application.ExeName)+'config') then begin
    Application.MessageBox('System config file is not exist!!!','Error',16);
    Application.Terminate;
  end;

  labCompanyName.Caption:=CompanyName;
  labsftname.caption:=SoftWareName;
  DoubleBuffered:=true;

  btnMenu7.Caption:='System Settings';
  btnMenu8.Caption:='Worker Manager';
  btnMenu6.Caption:='Punnect Manager';
  btnMenu5.Caption:='Issue Windows';
  btnMenu4.Caption:='Summary Reports';
  btnMenu3.Caption:='Graph For QC';
  btnMenu2.Caption:='Calc Payment';
  btnMenu1.Caption:='Database Backup';
  btnMenu9.Caption:='About System';
  {$IFNDEF DEBUG}
  try
    LoginComm:=TComm.Create(LoginPort,ctLogin);
    LoginComm.OnGetRFID:=LoginGetRFID;
    IssueComm:=TComm.Create(IssuePort,ctIssue);
    IssueComm.OnGetRFID:=IssueGetRFID;
  except
    Application.MessageBox('Serial port initialize fail please reset it in config file!','Port Error',16);
  end;
  {$ENDIF}
end;

procedure Tmainfrm.FormClose(Sender: TObject; var Action: TCloseAction);
var
  nIco:TNotifyIconData ;
begin
  nIco.cbSize:=  SizeOf( TNotifyIconData );
  nIco.Wnd:=Handle;
  nIco.hIcon:=MainFrm.Icon.Handle;
  nIco.uID:=ICO_ID;
  nIco.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
  nIco.uCallbackMessage:=WM_ICONBACK;
  nIco.szTip:=sIcoHint;
  Shell_NotifyIcon(NIM_DELETE,@nIco);
end;

procedure Tmainfrm.N81Click(Sender: TObject);
begin
  Close;
end;

procedure Tmainfrm.btnMenu1Click(Sender: TObject);
begin
  actDataBackupExecute(Sender);
end;

procedure Tmainfrm.btnMenu2Click(Sender: TObject);
begin
  actPaymentExecute(Sender);
end;

procedure Tmainfrm.btnMenu3Click(Sender: TObject);
begin
  actGraphQcExecute(Sender);
end;

procedure Tmainfrm.btnMenu4Click(Sender: TObject);
begin
  actSummRptExecute(Sender);
end;

procedure Tmainfrm.btnMenu5Click(Sender: TObject);
begin
  actIssueExecute(Sender);
end;

procedure Tmainfrm.btnMenu6Click(Sender: TObject);
begin
  actPntMngExecute(Sender);
end;

procedure Tmainfrm.btnMenu7Click(Sender: TObject);
begin
  actSysSetExecute(Sender);
end;

procedure Tmainfrm.btnMenu8Click(Sender: TObject);
begin
  actWkMngExecute(Sender);
end;

procedure Tmainfrm.btnMenu9Click(Sender: TObject);
begin
  actAboutExecute(Sender);
end;

procedure Tmainfrm.actSysSetExecute(Sender: TObject);
begin
  //
end;

procedure Tmainfrm.actWkMngExecute(Sender: TObject);
begin
  WorkerManage(pal_frmparent);
end;

procedure Tmainfrm.actPntMngExecute(Sender: TObject);
begin
  PunnetManage(pal_frmparent);
end;

procedure Tmainfrm.actIssueExecute(Sender: TObject);
begin
  IssuePunnet(pal_frmparent);
end;

procedure Tmainfrm.actPaymentExecute(Sender: TObject);
begin
  //
end;

procedure Tmainfrm.actSummRptExecute(Sender: TObject);
begin
  RecordsManage(pal_frmparent);
end;

procedure Tmainfrm.actGraphQcExecute(Sender: TObject);
begin
  //
end;

procedure Tmainfrm.actDataBackupExecute(Sender: TObject);
begin
  //
end;

procedure Tmainfrm.actAboutExecute(Sender: TObject);
var
  worker:TWorker;
begin
  worker:=TWorker.Create('134123');
  worker.StartWork;
end;

procedure Tmainfrm.btnMenu9LabMenuClick(Sender: TObject);
begin
  btnMenu9.LabMenuClick(Sender); 
end;

end.
